/* output.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "cgi.h"
#include "cookie.h"


int main() {
    sqlite3  *db  = NULL;
    int      nrow=0,ncolumn=0;
    char     **azResult = NULL;
    char     *zErrMsg = 0;
    char     sql[500];
    char     *input;
    char     *req_method;
    int      rc;

    if(isvalid_cookie()!=0){
        return 0;
    }

    printf("Content-type:text/json;charset=UTF-8\n\n");

    rc = sqlite3_open(PATH_DATABASE,&db);
    if (rc) {
        fprintf(stderr,"Can't open database:%s\n",sqlite3_errmsg(db));
        sqlite3_close(db);
        return 0;

    } else {

        // get request parameter
        req_method = getenv("REQUEST_METHOD");
        input = getcgidata(stdin, req_method);

        PARAM username = getParam(input, "username");
        PARAM password = getParam(input, "password");

        sprintf(sql, " update [tb_user] set pwd = '%s' where name = '%s' ", password.value, username.value);

        //printf("Content-type:text/html;charset=utf-8\n\n");
        //printf("%s", sql);

        int sr = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
        printf("{\"res\":\"%d\",\"msg\":\"%s\"}", sr, sqlite3_errmsg(db));

    }

    sqlite3_close(db);

    return 0;
}
